
if(sessionStorage.getItem("mainCustomertitle")=="编辑"){
    querydatas()
}

function querydatas() {
    $.ajax({
        url:root.member.findCustomerPictureById ,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data:{
            "id":sessionStorage.getItem("mainpicid"),

        },
        success: function (data) {
            if(data.errorCode==0){
               $("#picTitle").val(data.data.picTitle)
                console.log($("input[name=picPlatform]")[0])
                if(data.data.picPlatform==10){
                    $("#picPlatform1").prop("checked",true);
                }else if(data.data.picPlatform==20){
                    $("#picPlatform2").prop("checked",true);
                }
                $("#picOrder").val(data.data.picOrder)
                $("#pcUrl").attr("src",data.data.picUrl)
                $("#picPath").val(data.data.picPath)
                if(data.data.picStatus==10){
                    $("#picStatus1").prop("checked",true);
                }else if(data.data.picStatus==20){
                    $("#picStatus2").prop("checked",true);
                }


            }else {
                $.Huimodalalert(data.msg,2000);
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

function addBtn(cen) {
    $(cen).prev().trigger('click');
}
var imgs=''
var imgverification=false//验证是否是选择的图片
var formData = new FormData();
// 提交上传图片
function getupfilerul(can,e){
    var formData = new FormData();
    formData.append('file',$(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            for(var i=0; i<data.length; i++){
                $(can).prev().val(data[0].reletivePath)
                $(can).prev().text(data[0].reletivePath)
            }
            $("#fileUpone").attr('type','text');
            $("#fileUpone").attr('type','file');
            imgverification=true
            imgs=data[0].fullPath
        }
    })
}
// 上传图片返现
function putDataToPage(e,can) {
    //验证
    verificationPicFileType(can)
    for(var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if(!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src= data;
            img.onload = function(){
                var width = img.width;
                var height = img.height;
                if($("input:radio[name='picPlatform']:checked").val()==undefined){
                    $.Huimodalalert('请先选择端口',2000);
                    file.value = "";
                    return ;
                }else if ($("input:radio[name='picPlatform']:checked").val()==10){
                    /*PC图片*/
                    if (width != 740 | height != 460){
                        $.Huimodalalert('PC图片尺寸限制为：740px*460px',2000);
                        file.value = "";
                        return ;
                    }else {
                        $(can).parent().prev().children().attr("src", e.target.result);
                        $(can).next().text('更换图片')
                        getupfilerul(can,e)
                    }
                }else if ($("input:radio[name='picPlatform']:checked").val()==20){
                    /*APP图片*/
                    if (width != 750 | height != 400){
                        $.Huimodalalert('PC图片尺寸限制为：750px*400px',2000);
                        file.value = "";
                        return ;
                    }else {
                        $(can).parent().prev().children().attr("src", e.target.result);
                        $(can).next().text('更换图片')
                        getupfilerul(can,e)
                    }
                }




            };


        }
    }
    formData.append('file', $(can)[0].files[0]);
}
// 图片类型验证
function verificationPicFileType(file) {
    var flag=false;
    var fileTypes = [".jpg", ".png"];
    var filePath = file.value;
    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
    if(filePath){
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag= true;
                break;
            }
        }
        if (!isNext){
            $.Huimodalalert('图片格式jpg、png',2000);
            file.value = "";
            flag=false;
        }
    }
    return flag;
}


//切换端口
var switchPortdata=0//记录添加时是否第一次选中
function switchPort(val) {
    switchPortdata++;
    console.log(switchPortdata)
    if(sessionStorage.getItem("mainCustomertitle")=="添加"&& switchPortdata!=1){
        if(imgs!=""){
            var curInd = layer.confirm(
                '<span>由于端口轮播图尺寸不一致，切换端口会删除原有图片，是否切换</span>'+'<br/>', {offset:'230px',title:'提示',btn: ['确定', '取消']}, function(index){
                    imgs=''
                    $("#pcUrl").attr("src","")
                    layer.close(curInd);
                },function(){
                    if(val==1){
                        $("#picPlatform2").prop("checked",true);
                    }else if(val==2){
                        $("#picPlatform1").prop("checked",true);
                    }
                    layer.close(curInd);

                });
        }

    }
    if(sessionStorage.getItem("mainCustomertitle")=="编辑"){
        var picurl=''
        if(imgverification==true){
            picurl=imgs
        }else {
            picurl=$("#pcUrl").attr("src")

        }
        if(picurl!=""){
            var curInd = layer.confirm(
                '<span>由于端口轮播图尺寸不一致，切换端口会删除原有图片，是否切换</span>'+'<br/>', {offset:'230px',title:'提示',btn: ['确定', '取消']}, function(index){
                    imgs=''
                    $("#pcUrl").attr("src","")
                    layer.close(curInd);
                },function(){
                    if(val==1){
                        $("#picPlatform2").prop("checked",true);
                    }else if(val==2){
                        $("#picPlatform1").prop("checked",true);
                    }
                    layer.close(curInd);

                });
        }

    }



}
//保存
function addcarouselMap() {
    var picurl=""
    var carouselmapurl=""
    var picIds=""
    if(sessionStorage.getItem("mainCustomertitle")=="添加"){
        picurl=imgs
        carouselmapurl=root.member.addCustomerPicture
        picIds=""
    }else {
        if(imgverification==true){
            picurl=imgs
        }else {
            picurl=$("#pcUrl").attr("src")

        }
        carouselmapurl=root.member.updateCustomerPicture
        picIds=sessionStorage.getItem("mainpicid")
    }
    if($("#picTitle").val()==""){
        $.Huimodalalert("请编辑标题",2000);
        return false;
    }
    if($("input[name=picPlatform]:checked").val()== undefined){
        $.Huimodalalert("请编辑端口",2000);
        return false;
    }
    if($("#picOrder").val()==""){
        $.Huimodalalert("请编辑展示顺序",2000);
        return false;
    }

    if(picurl==""){
        $.Huimodalalert("请编辑图片",2000);
        return false;
    }
    if($("input[name=picStatus]:checked").val()==undefined){
        $.Huimodalalert("请编辑生效状态",2000);
        return false;
    }



    $.ajax({
        url:carouselmapurl,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "picId":picIds,
            "mainCustomerId":sessionStorage.getItem("mainCustomerIds"),
            "picTitle":$("#picTitle").val(),
            "picPlatform":$("input[name=picPlatform]:checked").val(),
            "picUrl":picurl,
            "picStatus":$("input[name=picStatus]:checked").val(),
            "picPath":$("#picPath").val(),
            "picOrder":$("#picOrder").val(),
            "isDelete":0,
            "userName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
        }),
        success: function (data) {
                    if(data.errorCode==0){
                        if(sessionStorage.getItem("mainCustomertitle")=="添加"){
                            layer.msg('添加成功', {icon: 6, time: 1000});
                            setTimeout(function () {
                                window.parent.searchcarouselMaps();
                                var index = parent.layer.getFrameIndex(window.name);  // 关闭当前页面，并不刷新父级页面
                                parent.layer.close(index);
                            }, 1000)
                        }else {
                            layer.msg('编辑成功', {icon: 6, time: 1000});
                            setTimeout(function () {
                                window.parent.searchcarouselMaps();
                                var index = parent.layer.getFrameIndex(window.name);  // 关闭当前页面，并不刷新父级页面
                                parent.layer.close(index);
                            }, 1000)
                        }

                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//取消
function cancelbtn() {
    var index = parent.layer.getFrameIndex(window.name);  // 关闭当前页面，并不刷新父级页面
    parent.layer.close(index);
}
